

package com.nn.smart.bus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.nn.smart.bus.commons.mybatis.service.impl.BaseServiceImpl;
import com.nn.smart.bus.commons.tools.constant.Constant;
import com.nn.smart.bus.commons.tools.page.PageData;
import com.nn.smart.bus.commons.tools.utils.ConvertUtils;
import com.nn.smart.bus.commons.tools.constant.FieldConstant;
import com.nn.smart.bus.commons.tools.utils.Result;
import com.nn.smart.bus.dao.NndzCarDictDao;
import com.nn.smart.bus.dto.NndzCarDictDTO;
import com.nn.smart.bus.entity.NndzCarDictEntity;
import com.nn.smart.bus.redis.NndzCarDictRedis;
import com.nn.smart.bus.service.NndzCarDictService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 定制模块字典表
 *
 * @author nanning nanning@123.com
 * @since v1.0.0 2021-08-25
 */
@Slf4j
@Service
public class NndzCarDictServiceImpl extends BaseServiceImpl<NndzCarDictDao, NndzCarDictEntity> implements NndzCarDictService {

    @Autowired
    private NndzCarDictRedis nndzCarDictRedis;

    @Override
    public PageData<NndzCarDictDTO> page(Map<String, Object> params) {
        IPage<NndzCarDictEntity> page = baseDao.selectPage(
                getPage(params, FieldConstant.CREATED_TIME, false),
                getWrapper(params)
        );
        return getPageData(page, NndzCarDictDTO.class);
    }

    @Override
    public List<NndzCarDictDTO> list(Map<String, Object> params) {
        List<NndzCarDictEntity> entityList = baseDao.selectList(getWrapper(params));

        return ConvertUtils.sourceToTarget(entityList, NndzCarDictDTO.class);
    }

    private QueryWrapper<NndzCarDictEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get(FieldConstant.ID_HUMP);
        String pid = (String) params.get("pid");
        String dictType = (String) params.get("dictType");
        String dictName = (String) params.get("dictName");
        String dictValue = (String) params.get("dictValue");
;
        QueryWrapper<NndzCarDictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), FieldConstant.ID, id);
        wrapper.eq(StringUtils.isNotBlank(pid), "pid", pid);
        wrapper.like(StringUtils.isNotBlank(dictType), "dict_type", dictType);
        wrapper.like(StringUtils.isNotBlank(dictName), "dict_name", dictName);
        wrapper.like(StringUtils.isNotBlank(dictValue), "dict_value", dictValue);
        wrapper.orderByAsc("sort");

        return wrapper;
    }

    @Override
    public NndzCarDictDTO get(String id) {
        NndzCarDictEntity entity = baseDao.selectById(id);
        return ConvertUtils.sourceToTarget(entity, NndzCarDictDTO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(NndzCarDictDTO dto) {
        NndzCarDictEntity entity = ConvertUtils.sourceToTarget(dto, NndzCarDictEntity.class);
        insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NndzCarDictDTO dto) {
        NndzCarDictEntity entity = ConvertUtils.sourceToTarget(dto, NndzCarDictEntity.class);
        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(String[] ids) {
        // 逻辑删除（@TableLogic 注解）
        baseDao.deleteBatchIds(Arrays.asList(ids));
    }

    @Override
    public List<NndzCarDictDTO> subList(Map<String, Object> params) {
        List<NndzCarDictEntity> entityList = baseDao.getSubList(params);
        return ConvertUtils.sourceToTarget(entityList, NndzCarDictDTO.class);
    }

    @Override
    public void delete(String id) {
        NndzCarDictDTO dto = this.get(id);
        nndzCarDictRedis.removeSimpleDictList(dto.getDictType());
        baseDao.deleteById(id);
    }

    @Override
    public Result<List<NndzCarDictDTO>> listSimpleDictInfo(String dictType) {
        List<NndzCarDictDTO> simpleDictList = Lists.newArrayList();

        try {
            if (StringUtils.isNotBlank(dictType)) {

                List<NndzCarDictDTO> dictList = nndzCarDictRedis.getSimpleDictList(dictType);
                if (null != dictList) {
                    return new Result<List<NndzCarDictDTO>>().ok(dictList);
                }

                QueryWrapper<NndzCarDictEntity> sdWrapper = new QueryWrapper<>();
                sdWrapper.select("dict_value", "dict_name");
                sdWrapper.eq("dict_type", dictType);
                sdWrapper.ne("dict_value", "");
                sdWrapper.isNotNull("dict_value");
                sdWrapper.orderByAsc("SORT");
                List<NndzCarDictEntity> entityList = baseDao.selectList(sdWrapper);
                if (!entityList.isEmpty()) {
                    simpleDictList = ConvertUtils.sourceToTarget(entityList, NndzCarDictDTO.class);
                    nndzCarDictRedis.setSimpleDictList(dictType, simpleDictList);
                }
            }
        } catch (Exception e) {
            log.error("获取数据字典列表失败，错误信息{}", e.getMessage());
        }
        return new Result<List<NndzCarDictDTO>>().ok(simpleDictList);
    }

}
